from app import app
from flask import render_template, redirect, flash
from forms import LoginForm, DBCreateForm

print "Importing routes"
@app.route('/')
@app.route('/index')
def index():
    user = { 'nickname': 'Miguel' } # fake user
    return render_template("index.html",
                           title = 'Home',
                           user = user)

@app.route('/noindex')
def noindex():
    user = { 'nickname': 'Cornell' }
    return render_template("index.html",
                            user = user)

@app.route('/forloop')
def forloop():
    user = { 'nickname': 'Miguel'}
    posts = [
        {
            'author': {'nickname': 'John'},
            'body': 'Beatiful day in the Portland!'
        },
        {
            'author': {'nickname': 'Susan'},
            'body': 'Avengers was a good movie'
        }
    ]
    return render_template("forloop.html",
                          user = user,
                          posts = posts)

@app.route('/login', methods = ['GET', 'POST'])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        flash('Login requested for OpenID="' + form.openid.data + '", remember_me=' +  str(form.remember_me.data))
        return redirect('index')
    return render_template('login.html',
                           title = "Sign In",
                           form = form,
                           providers = app.config['OPENID_PROVIDERS'])

@app.route('/user_create', methods = ['GET', 'POST'])
def user_create():
    form = DBCreateForm()
    if form.is_submitted():
        print "Submitted"
    if form.validate():
        print  "valid"
    else:
        print form.errors
    if form.validate_on_submit():
        print "Successful"
        return redirect('index')

    return render_template('user.html',
                            title = "Create User",
                            form = form) 
                                    